package com.geilizu.dom.service.impl;

import com.geilizu.dom.dto.*;
import com.geilizu.dom.entity.DormitoryAdmin;
import com.geilizu.dom.dao.DormitoryAdminDao;
import com.geilizu.dom.service.DormitoryAdminService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (DormitoryAdmin)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 15:08:44
 */
@Service("dormitoryAdminService")
public class DormitoryAdminServiceImpl implements DormitoryAdminService {
    @Resource
    private DormitoryAdminDao dormitoryAdminDao;



    @Override
    public DormitoryAdmin login(String username, String password) {
        return dormitoryAdminDao.login(username,password);
    }



    @Override
    public boolean save(DormitoryAdmin dormitoryAdmin){
        return dormitoryAdminDao.save(dormitoryAdmin);
    }

    @Override
    public Map<String,Object> list(Integer page, Integer size){
        if (page == null || size == null || page < 0) {
            page = 0;
            size = 3;
        }
        page = (page - 1) * 3;
        List<DormitoryAdmin> dormitoryAdminList = dormitoryAdminDao.queryAll(page, size);
        Long count = dormitoryAdminDao.queryAllCount();
        Map<String, Object> map = new HashMap<>();
        map.put("dormitoryAdminList", dormitoryAdminList);
        map.put("count", count);
        return map;
    }


    @Override
    public Map<String, Object> search(SearchDto searchDto) {
        Integer page = (searchDto.getPage() - 1) * searchDto.getSize();
        List<DormitoryAdmin> adminList = new ArrayList<>();
        Long count = Long.valueOf(0);
        if (searchDto.getKey().equals("username")) {
            SearchDoAdminDto search=new SearchDoAdminDto(page,searchDto.getSize(),searchDto.getValue(),"","");
            adminList = dormitoryAdminDao.search(search);
            count = dormitoryAdminDao.searchCount(search);
        } else if (searchDto.getKey().equals("name")) {
            SearchDoAdminDto search=new SearchDoAdminDto(page,searchDto.getSize(),"",searchDto.getValue(),"");
            adminList = dormitoryAdminDao.search(search);
            count = dormitoryAdminDao.searchCount(search);
        }else if(searchDto.getKey().equals("telephone")){
            SearchDoAdminDto search=new SearchDoAdminDto(page,searchDto.getSize(),"","",searchDto.getValue());
            adminList = dormitoryAdminDao.search(search);
            count = dormitoryAdminDao.searchCount(search);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("doAdminList", adminList);
        map.put("count", count);
        return map;
    }

    @Override
    public Integer deleteById(Integer id) {
        return dormitoryAdminDao.deleteById(id);
    }

    @Override
    public Integer updateById(DormitoryAdmin dormitoryAdmin) {
        return dormitoryAdminDao.updateById(dormitoryAdmin);
    }

    @Override
    public List<DormitoryAdmin> list() {
        return dormitoryAdminDao.list();
    }

    @Override
    public DormitoryAdmin findById(Integer id) {
        System.out.println(id);

        return dormitoryAdminDao.findById(id);
    }
}
